package com.smartwebee.android.blespp.hospital.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.WindowManager;

import com.android.volley.RequestQueue;
import com.smartwebee.android.blespp.utils.HttpLoading;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by steward on 2018/7/7.
 */

public class BaseFragment extends Fragment {
    protected String TAG = "BaseActivity";
    protected AlertDialog dialog;
    protected RequestQueue requestQueue;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isUseEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    protected boolean isUseEventBus() {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isUseEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        if (requestQueue != null) {
            requestQueue.cancelAll(TAG);
        }
    }

    public void showLoading() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setCancelable(false);
        builder.setView(new HttpLoading(getActivity()));
        dialog = builder.create();
        dialog.show();
        WindowManager m = getActivity().getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.7);    //宽度设置为屏幕的0.5
        dialog.getWindow().setAttributes(p);
    }

    public void dismissLoading() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
